﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Configuration;
using Newtonsoft.Json;
using System.Web.Script.Services;
using System.Data;

namespace PromotionEngine
{
    public partial class SalesPerson : System.Web.UI.Page
    {
        public class Response
        {
            public List<Model.SalesPerson> Data { get; set; }
            public int Count { get; set; }
            public string Errors { get; set; }

            public Response(List<Model.SalesPerson> data, int count)
            {
                this.Data = data;
                this.Count = count;
            }

            public Response(string errors)
            {
                this.Errors = errors;
            }

            public Response()
            {

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static Response GetSalesPersonList()
        {
            List<Model.SalesPerson> lstSalesPerson = new List<Model.SalesPerson>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllSalesPerson(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString(),
            bool.Parse(HttpContext.Current.Session["IsAdmin"].ToString()));
            if (dsPromotion != null)
            {
                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.SalesPerson salesPerson = new Model.SalesPerson();
                    salesPerson.CompanyCode = r["CompanyCode"].ToString();
                    salesPerson.CompanyName = r["CompanyName"].ToString();
                    salesPerson.SalesPersonCode = r["SalesPersonCode"].ToString();
                    salesPerson.SalesPersonName = r["SalesPersonName"].ToString();
                    salesPerson.SalesPersonID = r["SalesPersonID"].ToString();
                    salesPerson.ManagerCode = r["ManagerCode"].ToString();
                    salesPerson.ManagerName = r["ManagerName"].ToString();
                    salesPerson.ManagerID = r["ManagerID"].ToString();
                    salesPerson.CreatedUserID = r["CreatedUserID"].ToString();
                    lstSalesPerson.Add(salesPerson);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCustomerProperty = lstSalesPerson };
                return new Response(lstSalesPerson, dsPromotion.Tables[0].Rows.Count);
            }
            return new Response();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void UpdateSalesPerson(string oSalesPerson, bool isInsert)
        {
            Model.SalesPerson salesPerson = JsonConvert.DeserializeObject<Model.SalesPerson>(oSalesPerson);
            if (isInsert)
            {
                if (HttpContext.Current.Session["LoginUser"] != null)
                {
                    DataTable tbUser = (DataTable)HttpContext.Current.Session["LoginUser"];

                    if (tbUser != null && tbUser.Rows.Count > 0)
                    {
                        salesPerson.CreatedUserID = tbUser.Rows[0]["UserID"].ToString();
                        salesPerson.CompanyCode = tbUser.Rows[0]["CompanyCode"].ToString();
                    }
                }
            }
            Promotion pmt = new Promotion();
            string errMsg = pmt.UpdateJsonSalesPerson(salesPerson.SalesPersonID, salesPerson.SalesPersonCode, salesPerson.SalesPersonName, salesPerson.CompanyCode, salesPerson.CreatedUserID,
                salesPerson.ManagerID, ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), isInsert);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static void DeleteSalesPerson(string salesPersonID)
        {
            Promotion pmt = new Promotion();
            string errMsg = pmt.DeleteJsonSalesPerson(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), salesPersonID);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static string GetSalesPersonForManager()
        {
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllSalesPerson(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString(),
            bool.Parse(HttpContext.Current.Session["IsAdmin"].ToString()));
            if (dsPromotion != null)
            {
                return JsonConvert.SerializeObject(dsPromotion.Tables[0]);
            }
            return string.Empty;
        }
    }
}